/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ta.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.ta.entity.Liucheng;
import com.thinkgem.jeesite.modules.ta.service.LiuchengService;
import com.thinkgem.jeesite.modules.ta.util.WebServiceUtil;

/**
 * 流程Controller
 * @author 刘雨轩
 * @version 2017-06-05
 */
@Controller
@RequestMapping(value = "${adminPath}/ta/liucheng")
public class LiuchengController extends BaseController {

	@Autowired
	private LiuchengService liuchengService;
	
	@ModelAttribute
	public Liucheng get(@RequestParam(required=false) String id) {
		Liucheng entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = liuchengService.get(id);
		}
		if (entity == null){
			entity = new Liucheng();
		}
		return entity;
	}
	
	@RequiresPermissions("ta:liucheng:view")
	@RequestMapping(value = {"list", ""})
	public String list(Liucheng liucheng, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Liucheng> page = liuchengService.findPage(new Page<Liucheng>(request, response), liucheng); 
		User user = UserUtils.getUser();
		if(user == null){
			return "modules/sys/sysLogin";
		}
		String userId = user.getNo();
		List<String> ls = liuchengService.findQName(userId);
		model.addAttribute("ls", ls);
		model.addAttribute("page", page);
		return "modules/ta/liuchengList";
	}

	@RequiresPermissions("ta:liucheng:view")
	@RequestMapping(value = "form")
	public String form(Liucheng liucheng, Model model) {
		model.addAttribute("liucheng", liucheng);
		return "modules/ta/liuchengForm";
	}

	@RequiresPermissions("ta:liucheng:edit")
	@RequestMapping(value = "save")
	public String save(Liucheng liucheng, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, liucheng)){
			return form(liucheng, model);
		}
		liuchengService.save(liucheng);
		addMessage(redirectAttributes, "保存流程成功");
		return "redirect:"+Global.getAdminPath()+"/ta/liucheng/?repage";
	}
	
	@RequiresPermissions("ta:liucheng:edit")
	@RequestMapping(value = "delete")
	public String delete(String iid,HttpServletRequest request, RedirectAttributes redirectAttributes) {
		iid = "('"+iid+"')";
		WebServiceUtil.invokeRemoteFuc(iid, request);
		addMessage(redirectAttributes, "删除流程成功");
		return "redirect:"+Global.getAdminPath()+"/ta/liucheng/?repage";
	}

}